import { createStore } from 'vuex'
import { userInfo } from '@/api'
import { setToken } from '@/service/cookie'
const store = createStore({
  state: {
    from: undefined,
    userInfo: {}
  },
  mutations: {
    setFromRoute: (state, payload) => {
      state.from = payload
    },
    updateUserInfo: (state, payload) => {
      state.userInfo = payload
    }
  },
  actions: {
    addCount ({ commit }, payload) {
      commit('setFromRoute', payload)
    },
    fetchUserInfo: async ({ commit }, payload) => {
      sessionStorage.setItem('temporary', '1')
      const res = await userInfo(payload)
      setToken('userId', res.id)
      commit('updateUserInfo', res)
    }
  },
  modules: {
  }
})

export default store
